package com.example.redcultural;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class News extends AppCompatActivity {
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.argb(30, 255, 255, 255));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            getWindow().setNavigationBarColor(Color.WHITE);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR |
                View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        listView=findViewById(R.id.list_news);
        NewsGet info=new NewsGet();
        JSONObject jsonObject = null;
        JSONArray jsonArray_info=null;
        try {
             jsonObject=new JSONObject(info.get("top"));
             JSONObject jsonObject1=jsonObject.getJSONObject("result");
             jsonArray_info=jsonObject1.getJSONArray("data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        List<NewContent>  newContents= new ArrayList<>();
        final List<String> html_url=new ArrayList<>();
        for (int i = 0; i < jsonArray_info.length(); i++) {
            NewContent newContent=new NewContent();
            try {
                JSONObject a=jsonArray_info.getJSONObject(i);
                newContent.setTitle(a.getString("title"));
                newContent.setImg(a.getString("thumbnail_pic_s"));
                html_url.add(a.getString("url"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            newContents.add(newContent);
        }
        listView.setAdapter(new NewAdapter(this, newContents));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Intent.ACTION_VIEW);    //为Intent设置Action属性
                intent.setData(Uri.parse( html_url.get(position))); //为Intent设置DATA属性
                startActivity(intent);
            }
        });
    }
}
